package ua.org.nuos.sdms.clientgui.server.validators;

import com.vaadin.data.validator.AbstractValidator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 14.11.11
 * Time: 21:52
 * To change this template use File | Settings | File Templates.
 */
public class EmailValidator extends AbstractValidator {

    private static final String EMAIL_REGEXP = "[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)" +
            "*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+(?:[A-Z]{2}|ua|ru|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\\b";

    public EmailValidator(String errorMessage) {
        super(errorMessage);
    }

    @Override
    public boolean isValid(Object o) {
        String email = o.toString();
        Pattern pattern = Pattern.compile(EMAIL_REGEXP);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }
}